<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 * Adminlog Controller
 * Build by Phuc Nguyen
 * Contact : nguyenvanphuc0626@gmail.com
 */

class PaymentDebts_model extends MY_Model{
	
	public function __construct(){
		parent::__construct();
		$this->table_name = 'book_debts';
                $this->load->library('bookinglib');
                $this->bookinglib = new bookinglib();
	}
        
        public function selectListUnActive(){
            $this->db->select('count(*) AS count');
            $this->db->from($this->table_name);
            $this->db->where('payment_confirm', '0');
            $query = $this->db->get();
            
            return $query->result();
        }


        public function get_active_list(){
            return $this->active_list;
        }
        
        public function init_data($right){
		return array(
                                array(
						'name' => 'booking_id',
						'label' => $this->lang->line('booking_id'),
						'width' => '2%',
						'sort'  => 'DESC',
						'searchoptions' => FALSE
				),array(
						'name' => 'customer',
						'label' => $this->lang->line('customer'),
						'width' => '10%',
						'sort'  => FALSE,
						'searchoptions' => FALSE
				),array(
						'name' => 'booking_place_code',
						'label' => $this->lang->line('booking_place_code'),
						'width' => '2%',
						'sort'  => FALSE,
						'searchoptions' => FALSE
				),array(
						'name' => 'reason',
						'label' => $this->lang->line('reason'),
						'width' => '10%',
						'sort'  => FALSE,
						'searchoptions' => FALSE
				),array(
						'name' 	=> 'maturity',
						'label' => $this->lang->line('maturity'),
						'width' => '10%',
						'sort'  => FALSE,
						'searchoptions' => FALSE
				),array(
						'name' 	=> 'net_fare',
						'label' => $this->lang->line('net_fare'),
						'width' => '5%',
						'sort'  => FALSE,
						'searchoptions' => FALSE
				),array(
						'name' 	=> 'sale_fare',
						'label' => $this->lang->line('sale_fare'),
						'width' => '5%',
						'sort'  => FALSE,
						'searchoptions' => FALSE
				),array(
						'name' 	=> 'check',
						'label' => $this->lang->line('check'),
						'width' => '5%',
						'sort'  => FALSE,
						'searchoptions' => FALSE
				),array(
						'name' 	=> 'payment_confirm',
						'label' => $this->lang->line('payment_confirm'),
						'width' => '10%',
						'sort'  => FALSE,
						'searchoptions' => FALSE
				),array(
						'name' 	=> 'booker',
						'label' => $this->lang->line('booker'),
						'width' => '5%',
						'sort'  => FALSE,
						'searchoptions' => FALSE
				),array(
						'name' 	=> 'createdTime',
						'label' => $this->lang->line('createdTime'),
						'width' => '10%',
						'sort'  => FALSE,
						'searchoptions' => FALSE
				),array(
						'name' 	=> 'lastupdated',
						'label' => $this->lang->line('lastupdated'),
						'width' => '10%',
						'sort'  => FALSE,
						'searchoptions' => FALSE
				),
                                array(
						'name'  => 'button',
						'width' => '2%',
						'sort'  => FALSE,
						'label'  => '',
						'searchoptions' => FALSE
				)
		);
	}
	
	public function json_data($controller, $right){
		$this->datatables
		->select("tbl.id,tbl.booking_id,tbl.customerID,tbl.reason,tbl.debts,tbl.payment_confirm,tbl.payment_collect,tbl.amount,tbl.agent_code,tbl.lastupdated,blb.sale_fare,blb.net_fare,blb.booking_place_code,blb.createdTime AS ngaydat,usr.username AS booker,bc.name AS khachhang")
		->from($this->table_name.' AS tbl')
                ->join('book_list_booking AS blb','blb.id = tbl.booking_id','LEFT')
                ->join('users AS usr','usr.id = tbl.owner')
                ->join('book_contacts AS bc','bc.customerID = tbl.customerID')
                ->where('tbl.payment_collect = 0');
		//->unset_column('id');
	
		$this->datatables->set_produce_output(false);
		$ouput = $datatables = $this->datatables->generate();
		unset($ouput['aaData']);
		$ouput['aaData'] = array();             

		foreach($datatables['aaData'] as $item){
                        if($item['booking_place_code'] == "" || $item['ngaydat'] == ""){
                            $info = $this->paymentDebts_model->getInfoTable(array('booking_id'=>$item['booking_id']),'book_history');
                        }
			$ouput['aaData'][] = array(
                                        $item['booking_id'],
					$item['khachhang'],
                            		($item['booking_place_code'])?$item['booking_place_code']:$info[0]->booking_place_code,
                                        $item['reason'],
                                        date("d-m-Y",$item['debts']),
                                        ($item['net_fare'])?$this->bookinglib->my_number_format($item['net_fare'],2, ',', ','):$this->bookinglib->my_number_format($info[0]->net_fare,2, ',', ','),
                                        ($item['sale_fare'])?$this->bookinglib->my_number_format($item['sale_fare'],2, ',', ','):$this->bookinglib->my_number_format($info[0]->sale_fare,2, ',', ','),
                                        ($item['payment_confirm'] == 1)?'<span style="font-weight:bold;">Đã Duyệt</span>':'Chưa duyệt',
                                        ($item['payment_collect'] == 1)?'Đã Thanh Toán':'Chưa thanh toán',
                                        $item['booker'],
                                        ($item['ngaydat'])?date("d-m-Y H:i:s",$item['ngaydat']):date("d-m-Y H:i:s",$info[0]->createdTime),
                                        date("d-m-Y H:i:s",$item['lastupdated']),
					$this->add_button_payment($controller, $right, $item['id'])
			);
		}
	
		return json_encode($ouput);
	}

}